package com.ruralflow.activity.service.impl;

import java.util.List;
import com.ruralflow.common.utils.DateUtils;
import com.ruralflow.common.utils.SecurityUtils;
import com.ruralflow.common.utils.SequenceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruralflow.activity.mapper.ActivityMapper;
import com.ruralflow.activity.domain.Activity;
import com.ruralflow.activity.service.IActivityService;

/**
 * 社区活动Service业务层处理
 * 
 * @author ruralflow
 * @date 2025-02-24
 */
@Service
public class ActivityServiceImpl implements IActivityService 
{
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private SequenceUtils sequenceUtils;

    /**
     * 查询社区活动
     * 
     * @param activityId 社区活动主键
     * @return 社区活动
     */
    @Override
    public Activity selectActivityByActivityId(Long activityId)
    {
        return activityMapper.selectActivityByActivityId(activityId);
    }

    /**
     * 查询社区活动列表
     * 
     * @param activity 社区活动
     * @return 社区活动
     */
    @Override
    public List<Activity> selectActivityList(Activity activity)
    {
        return activityMapper.selectActivityList(activity);
    }

    /**
     * 新增社区活动
     * 
     * @param activity 社区活动
     * @return 结果
     */
    @Override
    public int insertActivity(Activity activity)
    {
        activity.setCreateTime(DateUtils.getNowDate());
        activity.setCreateBy(SecurityUtils.getUsername());
        activity.setActivityCode(getNo());
        return activityMapper.insertActivity(activity);
    }

    /**
     * 修改社区活动
     * 
     * @param activity 社区活动
     * @return 结果
     */
    @Override
    public int updateActivity(Activity activity)
    {
        activity.setUpdateTime(DateUtils.getNowDate());
        activity.setUpdateBy(SecurityUtils.getUsername());
        return activityMapper.updateActivity(activity);
    }

    /**
     * 批量删除社区活动
     * 
     * @param activityIds 需要删除的社区活动主键
     * @return 结果
     */
    @Override
    public int deleteActivityByActivityIds(Long[] activityIds)
    {
        String updateBy = SecurityUtils.getUsername();
        return activityMapper.deleteActivityByActivityIds(activityIds, updateBy);
    }

    /**
     * 删除社区活动信息
     * 
     * @param activityId 社区活动主键
     * @return 结果
     */
    @Override
    public int deleteActivityByActivityId(Long activityId)
    {
        String updateBy = SecurityUtils.getUsername();
        return activityMapper.deleteActivityByActivityId(activityId, updateBy);
    }

    /**
     * 生成社区活动编码
     *
     * @return 社区活动信息登记编码
     */
    private String getNo() {
        // 获取用户id补0到五位
        Long userId = SecurityUtils.getUserId();
        String userIdStr = String.format("%05d", userId); // 补零到5位
        return sequenceUtils.createSequenceNumber("ACY" + userIdStr);
    }
}
